package com.qiyee.dalligent;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Title: BarcodeExcelExport
 * Description:
 * Create on: 2024/12/19 17:43
 *
 * @author xt.ahs.shiyaning1
 * @version 1.0.0
 */
public class BarcodeExcelExport {
    private static final Integer CODE_WIDTH = 100;                 // 基础属性：二维码宽度，单位像素
    private static final Integer CODE_HEIGHT = 30;
    public static void main(String[] args) throws Exception {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Barcodes");

        String pic = createPic();

        for (int i = 0; i < 10; i++) {
            //byte[] barcodeData = canvas.getBuffer();


            // Create a picture
            Drawing drawing = sheet.createDrawingPatriarch();
            ClientAnchor anchor = drawing.createAnchor(1, 1, 1, 1, i * 2, 0, i * 2 + 1, 1);
            drawing.createPicture(anchor,
                workbook.addPicture(Files.readAllBytes(Paths.get(pic)), Workbook.PICTURE_TYPE_PNG));

        }

        // Write the output to a file
        try (FileOutputStream fileOut = new FileOutputStream("barcodes.xlsx")) {
            workbook.write(fileOut);
        }

        workbook.close();
    }
    public static String createPic() throws Exception{
        String qrMessage = "8894796215296711"+new Random().nextInt(100);

        // EncodeHintType：指定和控制码生成器的一些参数的类
        Map<EncodeHintType, Object> typeObjectHashMap = new HashMap<EncodeHintType, Object>();
        // 设置编码
        typeObjectHashMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 设置容错等级
        typeObjectHashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 设置边距，默认为5
        typeObjectHashMap.put(EncodeHintType.MARGIN, 3);

        // MultiFormatWriter:用于写入条形码或二维码
        Code128Writer multiFormatWriter = new Code128Writer();
        BitMatrix bitMatrix = multiFormatWriter
            .encode(qrMessage, BarcodeFormat.CODE_128, CODE_WIDTH, CODE_HEIGHT, typeObjectHashMap);

        // java.awt.image.BufferedImage：把条形码信息转换为图像信息
        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);;


        String path = qrMessage + ".png";
        ImageIO.write(bufferedImage, "png", new FileOutputStream(path));
        return path;
    }
}
